package com.songcha.library_business.proxy.database

import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.TimeUtil
import com.songcha.library_database_douyue.bean.BookTodayReadTimeDBBean
import com.songcha.library_database_douyue.greendao.BookTodayReadTimeDBBeanDao
import com.songcha.library_database_douyue.manager.GreenDaoManager

/***
 * 当天阅读时间
 */
class BookTodayReadTimeDaoManager {
    companion object{
        @JvmStatic
        fun addTodayReadTime(startTime:Long,endTime:Long):Int{
            val second=(endTime-startTime).toInt()
            if(second<0) return 0
            try{
                val dao=GreenDaoManager.getInstance().bookTodayReadTimeDBBeanDao
                val bean=BookTodayReadTimeDBBean()
                bean.userId=UserInfoManager.userId
                val qb=dao.queryBuilder().where(BookTodayReadTimeDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                val list=qb.list()
                if(list.size==0){
                    bean.readTime=second
                    bean.startReadTime=TimeUtil.getCurrentTimeSecond()
                    dao.insert(bean)
                    return bean.readTime or (1 shl 31)
                }else {
                    val item=list[0]
                    //whether today read
                    val todayZero=TimeUtil.getTodayZeroTimeStamp()
                    bean.id=item.id
                    if(item.startReadTime>=todayZero && item.startReadTime<todayZero+86400){
                        bean.readTime=item.readTime+second
                        bean.startReadTime=item.startReadTime
                        dao.update(bean)
                        return bean.readTime
                    }else{
                        //pick today read time
                        bean.readTime=(todayZero+86400-startTime).toInt()
                        bean.startReadTime=TimeUtil.getCurrentTimeSecond()
                        dao.update(bean)
                        return bean.readTime or (1 shl 31)
                    }

                }
            }catch (e:Exception){
                LogUtil.log("保存今天阅读时间异常")
                e.printStackTrace()
                return 0
            }

        }

        @JvmStatic
        fun getTodayReadTime():Int{
            try{
                val dao=GreenDaoManager.getInstance().bookTodayReadTimeDBBeanDao
                val qb=dao.queryBuilder().where(BookTodayReadTimeDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                val list=qb.list()
                if(list.size==0) return 0
                val item=list[0]
                val todayZero=TimeUtil.getTodayZeroTimeStamp()
                if(item.startReadTime>=todayZero && item.startReadTime<todayZero+86400){
                    return item.readTime
                }else{
                    val bean=BookTodayReadTimeDBBean()
                    bean.id=item.id
                    bean.userId=UserInfoManager.userId
                    bean.readTime=0
                    bean.startReadTime=TimeUtil.getCurrentTimeSecond()
                    dao.update(bean)
                    return 0
                }
            }catch (e:Exception){
                e.printStackTrace()
                return 0
            }
        }

    }
}